﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Common;
using Module;
using EMPower.Utility;
using BLL;

namespace App
{
    public partial class BBS_AddThread : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!AppAuthHelper.SessionCheck())
            {
                Community.Utility.JavaScriptHelper.ConfirmBox(this, "请登录后发表评论，确定登录？", AppAuthHelper.GetLoginUrl(), Contexts.AppPath + "/BBS/ThreadList.aspx?");
                return;
            }
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }
        protected void btn_OnClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txt_Subject.Text.Trim()))
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "主题不能为空");
                return;
            }
            if (this.txt_Subject.Text.Trim().Length > 80)
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "主题长度不能超过80");
                return;
            }
            if (string.IsNullOrEmpty(this.content.Value.Trim()))
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "正文不能为空");
                return;
            }
            //if (this.txt_Subject.Text.Trim().Length > 4000)
            //{
            //    Community.Utility.JavaScriptHelper.MessageBox(this, "正文长度不能超过4000");
            //    return;
            //}
            if (string.IsNullOrEmpty(this.hf_threadId.Value))
            {
                UserPaperInfo newPaper = new UserPaperInfo();
                newPaper.Body = this.content.Value.Trim();
                newPaper.DateCreated = DateTime.Now;
                newPaper.IsAllowComment = 1;
                newPaper.IsBBSPaper = 1;
                newPaper.IsTop = 0;
                newPaper.LastUpdate = DateTime.Now;
                newPaper.Status = 1;
                newPaper.Subject = this.txt_Subject.Text.Trim();
                newPaper.TotalReplay = 0;
                newPaper.TotalView = 0;
                newPaper.Type = 1;
                newPaper.UserPaperId = IdHelper.GetLongId();
                newPaper.UserPassportId = AppAuthHelper.CurrentUserPassportId;
                bool isOk = BLL.BUserPaper.getInstance().Insert(newPaper);
                if (isOk)
                {
                    Community.Utility.JavaScriptHelper.ConfirmBox(this, "发表帖子成功，继续发表？", Contexts.ThisPageUrlWithParams, Contexts.AppPath + "/BBS/ThreadList.aspx?");
                }
                else
                {
                    Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "发表失败，请重新发表！", Contexts.AppPath + "/BBS/ThreadList.aspx?");
                }
            }
            else
            {
                UserPaperInfo newPaper = ViewState["UserPaperInfo"] as UserPaperInfo;
                newPaper.Subject = this.txt_Subject.Text.Trim();
                newPaper.Body = this.content.Value.Trim();
                newPaper.LastUpdate = DateTime.Now;
                bool isOk = BLL.BUserPaper.getInstance().Update(newPaper);

                if (isOk)
                {
                    Community.Utility.JavaScriptHelper.ConfirmBox(this, "编辑帖子成功，点击确定返回帖子列表，点击取消进入编辑后的帖子？", Contexts.AppPath + "/BBS/ThreadList.aspx?", Contexts.AppPath + "/BBS/ThreadInfo.aspx?"+Contexts.ThreadId+"="+Contexts.GetEncrypt(newPaper.UserPaperId.ToString()));
                }
                else
                {
                    Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "发表失败，请重新发表！", Contexts.AppPath + "/BBS/ThreadList.aspx?");
                }
            }
        }


        private void pageInit()
        {
            long threadId = Contexts.GetAPPThreadId();
            if (threadId > 0)
            {
                UserPaperInfo paper = BUserPaper.getInstance().GetPaperInfo(threadId);
                if (paper != null && paper.UserPaperId > 0)
                {
                    ViewState["UserPaperInfo"] = paper;

                    this.txt_Subject.Text = paper.Subject;
                    this.content.Value = paper.Body;
                    hf_threadId.Value = paper.UserPaperId.ToString();
                }
                else
                {
                    Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "参数错误",  Contexts.AppPath + "/BBS/ThreadList.aspx?");
                    return;
                }
            }
        }
    }
}